package com.ruoyi.web.core.config;

import java.util.ArrayList;
import java.util.List;

import com.zhonglai.swagger.ZlDocket;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.ruoyi.common.config.RuoYiConfig;
import org.springframework.web.method.HandlerTypePredicate;
import org.springframework.web.servlet.config.annotation.PathMatchConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spring.web.plugins.Docket;

/**
 * Swagger2的接口配置
 *
 * @author ruoyi
 */
@Configuration
public class SwaggerConfig implements WebMvcConfigurer
{
    /** 系统基础配置 */
    @Autowired
    private RuoYiConfig ruoyiConfig;

    /** 是否开启swagger */
    @Value("${swagger.enabled}")
    private boolean enabled;

    private String pathMapping = null;

    private String basePackage = "com.ruoyi.web.controller";

    @Override
    public void configurePathMatch(PathMatchConfigurer configurer) {
        if(null != pathMapping)
        {
            // 添加 pathPrefix 给 com.ruoyi.web.controller.tool.api 包下的所有 Controller
            configurer.addPathPrefix(pathMapping, HandlerTypePredicate.forBasePackage(basePackage));
        }
    }
    /**
     * 创建API
     */
    @Bean
    public Docket createRuoYiAdminApi()
    {
        return new ZlDocket(enabled,apiInfo(),basePackage).pathMapping(pathMapping);

    }


    /**
     * 添加摘要信息
     */
    private ApiInfo apiInfo()
    {
        // 用ApiInfoBuilder进行定制
        return new ApiInfoBuilder()
                // 设置标题
                .title("管理员端接口")
                // 描述
                .description("描述：用于管理系统功能")
                // 作者信息
                .contact(new Contact(ruoyiConfig.getName(), null, null))
                // 版本
                .version("版本号:" + ruoyiConfig.getVersion())
                .build();
    }
}
